import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class PersonalIconSelectPageViewModel extends BaseViewModel {
  PublishSubject loadingState = PublishSubject<CommonLoadingState?>();
  final Color defaultColor = Colors.white;
  final Color selectColor = const Color(0xFF88B5FA);
  PhotoListResponse? _photoListResponse;

  PhotoListResponse? get photoListResp => _photoListResponse;


  Future<void> pullIconsList(
      {OnSuccess? onSuccess, OnFailure? onFailure}) async {
    Map<String, dynamic>? result =
        await getNetworkUtil().postHttp(UrlContract.pullIconsList);
    final resp = PhotoListResponse.fromMap(result);
    if (resp.status == 0) {
      _photoListResponse = resp;
      loadingState.add(CommonLoadingState.successTypeOne);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      loadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  Future<bool?> changeIcon(
    String iconId, {
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['icon_id'] = iconId;
    final result =
        await getNetworkUtil().postHttp(UrlContract.modifyUserIcon, params: params);
    final resp = CommonResponse.fromMap(result);
    if (resp.status == 0) {
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }

  Future<void> backToPage(BuildContext context) async {
    getApplication().getRouter()?.pop(context);
  }

  void resetColor() {
    _photoListResponse?.list?.forEach((element) {
      element?.photoColor = defaultColor;
    });
  }
}
